package com.unicom.mec.deploy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unicom.mec.deploy.common.ResultBean;
import com.unicom.mec.deploy.service.DeployService;
import com.unicom.mec.deploy.vo.ApplicationDeployVo;
import com.unicom.mec.deploy.vo.ApplicationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 应用部署控制层
 *
 * @author 刘亮武
 * @date 2020年06月17日
 */
@Api(value = "/deploy", tags = "应用部署管理")
@RestController
@RequestMapping("/deploy")
public class DeployController {

    @Autowired
    private DeployService deployService;

    @ApiOperation(value = "添加应用部署信息", httpMethod = "POST")
    @PostMapping("/")
    @ResponseBody
    ResultBean addApplicationDeploy(@RequestBody @Valid ApplicationDeployVo applicationDeployVo) {
        deployService.addApplicationDeploy(applicationDeployVo);
        ResultBean result = new ResultBean();
        return result;
    }

    @ApiOperation(value = "删除应用部署信息", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    @ResponseBody
    ResultBean deleteApplicationDeploy(@ApiParam(value = "主键") @PathVariable(value = "id") Long id) {
        deployService.deleteApplicationDeploy(id);
        ResultBean result = new ResultBean();
        return result;
    }

    @ApiOperation(value = "删除项目下所有应用",hidden = true)
    @DeleteMapping("/forProject/{projectId}")
    @ResponseBody
    ResultBean deleteApplicationDeployByProjectId(@ApiParam(value = "主键") @PathVariable(value = "projectId") Long projectId ) {
        deployService.deleteApplicationDeployByProjectId(projectId);
        ResultBean result = new ResultBean();
        return result;
    }

    @ApiOperation(value = "应用部署列表", httpMethod = "GET")
    @GetMapping("/")
    @ResponseBody
    ResultBean<Page<ApplicationVo>> findApplications(@ApiParam(value = "应用名称") @RequestParam(value = "applicationName", required = false) String applicationName,
                                                     @ApiParam(value = "项目主键") @RequestParam(value = "projectId") Long projectId,
                                                     @ApiParam(value = "页码") @RequestParam(value = "page") Integer page,
                                                     @ApiParam(value = "查询条数") @RequestParam(value = "pageSize") Integer pageSize) {
        Page<ApplicationVo> applicationVoPage = deployService.findApplications(applicationName, projectId, page, pageSize);
        ResultBean<Page<ApplicationVo>> result = new ResultBean<>();
        result.setData(applicationVoPage);
        return result;
    }
}
